#include <iostream>
#include <vector>

#include "cmdline/Parse.h"
#include "touch.h"
#include "touch_info.h"


using namespace std;
using namespace tinytool;


static void touch_cmdline_parse(int argc, char* argv[])
{
    cmdline::Parse parse;
    parse.add("force", 'f', "override the exist file");
    parse.add("recur", 'r', "create the not existed directory");
    parse.add_more<string>("input", 'i', "touch inputting files", true, true);
    parse.add_more<string>("namespace", 'n', "the namespaces written to the file");
    parse.add<string>("project", 'p', "the project name where the file is located");
    parse.set_program_name(TOUCH_PROGRAM_NAME);

    if (!parse.parse(argc, argv)) {
        if (parse.exist('s')) {
            std::cout << parse.show() << std::endl;
        }
        else if (parse.exist('h')) {
            std::cout << parse.usage() << std::endl;
        }
        else if (parse.exist('v')) {
            cout_log(parse.program_name(), TOUCH_VERSION);
        }
        else {
            cout_error(parse.program_name(), "cmdline parse errors");
            std::cout << parse.errors() << std::endl;
        }
    }
    else {
        if (parse.exist('s')) {
            std::cout << parse.show() << std::endl;
        }
        else if (parse.exist('h')) {
            std::cout << parse.usage() << std::endl;
        }
        else if (parse.exist('v')) {
            cout_log(parse.program_name(), TOUCH_VERSION);
        }
        else {
            auto path       = parse.get_values('i');
            auto namespaces = parse.get_values('n');
            auto project    = parse.get_value('p');
            bool is_force   = parse.exist('f');
            bool create_dir = parse.exist('r');
            for (auto it : path) {
                touch::touch(it, namespaces, project, is_force, create_dir);
            }
        }
    }
}

int main(int argc, char* argv[])
try {
    touch_cmdline_parse(argc, argv);
    return 0;
}
catch (const std::exception& e) {
    cout_error(TOUCH_PROGRAM_NAME, e.what());
    return 1;
}